Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Figure 12.5 The circle display after changing the form’s coordinate system. Try decreasing the form size; you’ll notice the circles are not redrawn. Why is this? The Paint event is triggered when an object’s size is increased or when it is uncovered; it is not triggered when an object’s size decreases. ### The Picture Box Control Now let’s look at Visual Basic’s most versatile graphics control: the Picture Box. The capabilities of this control fall into three areas: • Displaying pictures that were created elsewhere and exist in disk files, such as scanned photographs or drawings created with a paint program • Displaying graphics that are created with program statements, such as the Circle method • Serving as a container for grouping other controls The third category doesn’t relate to graphics, so we won’t discuss it further. The other two, while easy to describe, provide a wealth of graphical capabilities limited only by your imagination. Note that most of the graphics actions that apply to the Picture Box control can also be used with the Form object, which we’ll cover later in the chapter. Let’s see how the Picture Box works. TIP: Image Controls Vs. Picture Box Controls For displaying existing images, you may prefer to use an Image control rather than a Picture Box. Image controls have only a subset of the capabilities of Picture Box controls, and as a result, they are faster and consume fewer system resources. You’ll learn about the Image control later in the chapter. #### Loading A Picture During Program Design You can load a picture into a Picture Box during program design, with the effect of embedding the picture into the project’s executable file. (Pictures are often stored as FRX files.) This method is appropriate if a Picture Box will always display the same picture or if you don’t want the separate picture file to be distributed with the program. Loading a picture during program design does not prevent you from loading another image into the Picture Box while the program is running. You can load into a Picture Box the image formats shown in Table 12.3. To load a picture at design time, select the Picture Box’s Picture property in the Properties window. Visual Basic displays an Open dialog box, which allows you to select the picture file. The picture you select is displayed in the control during design, as well as when the program runs. Table 12.3 Image formats for a Picture Box.
Format | File Extension | Description
--- | --- | ---
Bitmap | .BMP or .DIB | Windows bitmap. Defines an image as a grid of dots (pixels).
Icon | .ICO | A special kind of bitmap, typically 32 by 32 or 16 by 16 pixels in size.
Cursor | .CUR | Similar to an icon, but also contains a defined hotspot that precisely tracks the cursor location. Use for the mouse pointer.
Metafile | .WMF or .EMF | Windows metafile. Defines an image in terms of lines and shapes.
JPEG | .JPG or .JPEG | Joint Photographic Expert Group format. A compressed image format that supports 8- and 24-bit color. Commonly used on the Internet.
GIF | .GIF | Graphical Interchange Format. A compressed format that supports only 8-bit color. Also popular on the Internet.
A picture is loaded into a Picture Box control with the top-left corner of the picture positioned in the top-left corner of the Picture Box. If the image is smaller than the Picture Box, the area outside the image displays the color specified by the Picture Box’s BackColor property. If the image is bigger than the control, the portion outside the Picture Box is clipped if the AutoSize property is set to False (the default). You can set the Picture Box’s AutoSize property to True, which causes the Picture Box to automatically grow or shrink to fit the image exactly. You can also load a picture into a Picture Box from the Windows Clipboard. Open the image in the application in which you created or edited it, then copy the image to the Clipboard using the Edit|Copy command. Switch back to Visual Basic, select the Picture Box control, and select Edit|Paste. #### Loading A Picture During Program Execution Several methods are available for displaying a picture in a Picture Box at runtime. One method is to set the control’s Picture property. You cannot do it directly, as you might expect:
    Pbox.Picture = filename        ‘ Does not work!
Rather, you must use the LoadPicture function
    Pbox.Picture = LoadPicture(filename)
in which filename is the name of a file in one of the supported image formats, including the full path, if necessary. The position of the picture and the effect of the control’s AutoSize property are the same as when you load a picture at design time. You can erase a Picture Box by using LoadPicture with no argument:
    Pbox.Picture = LoadPicture()
You can also copy a picture from one Picture Box to another by accessing the source control’s Picture property:
    Pbox1.Picture = Pbox2.Picture
Loading an image into a Picture Box with these methods is fine for most situations, but it does have a few limitations: • The image is always placed in the upper-left corner of the Picture Box. • The image size cannot be changed—that is, the image is always displayed with its original size. • Only one image at a time can be displayed in a given control. Fear not, you do have alternatives. The PaintPicture method, which we’ll discuss next, is one of them. #### The PaintPicture Method The PaintPicture method allows you to circumvent the limitations of the previous methods used to load a picture during program execution. Think of PaintPicture as a sophisticated cut-and-paste tool. You must start with a source picture, which can be an image in either a Picture Box control or on a form. Then you can take all or part of that picture—any rectangular region, in fact—and place it anywhere you desire on another Picture Box or form, or on the source Picture Box or form. You have numerous options as to how the copied image is combined with any image that already exists in the destination. In addition, you can stretch or shrink the “copied” picture.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.